package com.infitecs.eyas.mill;

import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.mill.model.Mill;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.MillSelectCalculate;
import com.infitecs.eyas.mill.model.SystemHeat;
import com.infitecs.eyas.mill.service.MillSelectCalculateService;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.MillService;
import com.infitecs.eyas.mill.service.SystemHeatService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/28 0028.
 */
@Controller
@RequestMapping("/mill/heat")
public class SystemHeatController {
    @Autowired
    private SystemHeatService systemHeatService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private MillSelectService millSelectService;

    @Autowired
    private MillSelectCalculateService millSelectCalculateService;

    @Autowired
    private MillService millService;

    @Autowired
    private AirGasParameterService airGasParameterService;

    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private StepService stepService;

    @Autowired
    private OperateFieldDao operateFieldDao;

    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getMillPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "mill/heat";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "mill/heat";
        }
        step = stepService.geMmjStep(caseId);
        if (step < 14) {
            request.setAttribute("step", step);
            return "mill/heat";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        MillSelect millSelect = millSelectService.getMillSelect();
        List<Mill> millList = millService.getMillList(millSelect.getE7().intValue());
        Map<String, Double> dataMap = millSelectService.getChooseDataByCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("dataMap", dataMap);
        request.setAttribute("millSelect", millSelect);
        request.setAttribute("millList", millList);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "mill/heat";
    }

    @RequestMapping(value = "/saveValueOfSystemHeat", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfSystemHeat(CalculateBurn calculateBurn) {
        systemHeatService.saveValueOfSystemHeat(calculateBurn);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putSystemHeatValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/getCalculate", method = RequestMethod.GET)
    @ResponseBody
    public SystemHeat getCalculate() {
        MillSelectCalculate millSelectCalculate = millSelectCalculateService.getSelectCalculate();
        MillSelect millSelect = millSelectService.getMillSelect();
        SystemHeat systemHeat = new SystemHeat();
        Map<String, Double> map = sessionService.getParamMap();
        if (null != millSelect && millSelectCalculate != null) {
            Double E7 = millSelect.getE7();
            String model = millSelectCalculate.getModel();
            switch (E7.intValue()) {
                case 8:
                    systemHeat.setE50(expressionService.calculate(new Param("AE51")));
                    break;
                case 9:
                    systemHeat.setE50(expressionService.calculate(new Param("AE51")));
                    break;
                default:
                    systemHeat.setE50(expressionService.calculate(new Param("AE50")));
            }
            Mill mill = millService.getMill(model,E7.intValue());
            if (E7.equals(7.0)) {
                //mps_hp_II取最大的通风
                Double ME3 = map.get("ME3");
                Double ME10 = map.get("ME10");
                if (ME10 > 37 && ME3 > 20) {
                    systemHeat.setE52(mill.getVentilationH());
                    map.put("AE52", mill.getVentilationH());
                } else {
                    systemHeat.setE52(mill.getVentilationY());
                    map.put("AE52", mill.getVentilationY());
                }


            } else {
                systemHeat.setE52(mill.getVentilation());
                map.put("AE52",mill.getVentilation());
            }
//            AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(sessionService.getCaseId());
//            if (airGasParameter != null) {
//                map.put("AE54",airGasParameter.getOnceWindRate());
//            } else {
//                map.put("AE54",millSelect.getE54());
//            }
            systemHeat.setE53(expressionService.calculate(new Param("AE53")));
            systemHeat.setE55(expressionService.calculate(new Param("AE55")));
            systemHeat.setE56(expressionService.calculate(new Param("AE56")));
            systemHeat.setE57(expressionService.calculate(new Param("AE57")));
        }
        return systemHeat;
    }

    @RequestMapping(value = "checkE59", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkE59() {
        Map<String, Double> map = sessionService.getParamMap();
        Double E58 = expressionService.calculate(new Param("AE58"));
        Double E52 = map.get("AE52");
        Double E53 = map.get("AE53");
        Double E56 = map.get("AE56");
        if (E52 >= E58 && E56 >= E53) {
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/calculateOther",method = RequestMethod.POST)
    @ResponseBody
    public SystemHeat calculateOther() {
        MillSelectCalculate millSelectCalculate = millSelectCalculateService.getSelectCalculate();
        MillSelect millSelect = millSelectService.getMillSelect();
        SystemHeat systemHeat = new SystemHeat();
        Map<String,Double> map = sessionService.getParamMap();
        Mill mill = millService.getMill(millSelectCalculate.getModel(),millSelect.getE7().intValue());
        expressionService.calculate(new Param("AE61"));
        expressionService.calculate(new Param("AE69"));
        System.out.println(expressionService.calculate(new Param("AE75")));
        expressionService.calculate(new Param("AE77"));
        expressionService.calculate(new Param("AE79"));
        expressionService.calculate(new Param("AE90"));
        systemHeat.setE65(expressionService.calculate(new Param("AE65")));
        systemHeat.setE66(expressionService.calculate(new Param("AE66")));
        systemHeat.setE70(expressionService.calculate(new Param("AE70")));
        systemHeat.setE71(expressionService.calculate(new Param("AE71")));
        System.out.println(expressionService.calculate(new Param("AE80")));;
        System.out.println(expressionService.calculate(new Param("AE81")));
        System.out.println(map.get("ME3"));
        System.out.println(map.get("AE63"));
        System.out.println(map.get("AE74"));
        systemHeat.setE82(expressionService.calculate(new Param("AE82")));
        System.out.println(map.get("AE82"));
        systemHeat.setE84(expressionService.calculate(new Param("AE84")));
        systemHeat.setE86(expressionService.calculate(new Param("AE86")));
        systemHeat.setE89(expressionService.calculate(new Param("AE89")));
        systemHeat.setE90(expressionService.calculate(new Param("AE90")));
        systemHeat.setE92(expressionService.calculate(new Param("AE92")));
        systemHeat.setE97(expressionService.calculate(new Param("AE97")));
        systemHeat.setE100(expressionService.calculate(new Param("AE100")));
        systemHeat.setE102(expressionService.calculate(new Param("AE102")));
        systemHeat.setE103(expressionService.calculate(new Param("AE103")));
        Double E107 = expressionService.calculate(new Param("AE107"));
        if (E107 != null) {
            systemHeat.setE107(E107);
        } else {
            systemHeat.setE107(mill.getSeal());
            map.put("AE107",mill.getSeal());
        }
        expressionService.calculate(new Param("AE106"));
        expressionService.calculate(new Param("AE95"));
        systemHeat.setE108(expressionService.calculate(new Param("AE108")));
        systemHeat.setE109(expressionService.calculate(new Param("AE109")));
        systemHeat.setE110(expressionService.calculate(new Param("AE110")));
        systemHeat.setE111(expressionService.calculate(new Param("AE111")));
        systemHeat.setE77(map.get("AE77"));
        systemHeat.setE79(map.get("AE79"));
        systemHeat.setE82(map.get("AE82"));
        systemHeat.setE84(map.get("AE84"));
        systemHeat.setE86(map.get("AE86"));
        systemHeat.setE89(map.get("AE89"));
        systemHeat.setE90(map.get("AE90"));
        systemHeat.setE105(map.get("AE105"));
        systemHeat.setE106(map.get("AE106"));
        return systemHeat;
    }

}
